package com.tjn;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class Exercise {
    public static void main(String[] args) {

    }
    @Test//JUnin，测试类，写这个可以单独运行某一个方法
    public void test1(){
        DAO<User> dao = new DAO<>();
        dao.save("1",new User("tjn",1,18));
        dao.save("2",new User("dy",2,19));
        dao.save("3",new User("syz",3,20));

        List<User> list = dao.list();
        for (User user : list) {
            System.out.println(user);
        }
        dao.update("1",new User("tjn",1,28));
    }
}

class DAO<T>{
    private Map<String,T> map = new HashMap<>();

    public void save(String id,T entity){
        map.put(id,entity);
    }
    public T get(String id){
        return map.get(id);
    }
    public void update(String id,T entity){
        map.put(id,entity);
    }
    public List<T> list(){
        return new ArrayList<>(map.values());
    }
    public void delete(String id){
        map.remove(id);
    }
}

class User{
    private String name;
    private int id;
    private int age;
    public User(String name,int id,int age){
        this.name = name;
        this.id = id;
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }

}